package com.example.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.management.common.QueryPageParam;
import com.example.management.common.Result;
import com.example.management.entity.Countproduct;
import com.example.management.service.CountproductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/countProduct")
public class CountproductController {
    @Autowired
    private CountproductService countproductService;
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);
        System.out.println("num==" + query.getPageNum());
        System.out.println("size==" + query.getPageSize());
        HashMap param = query.getParam();
        String no = (String) param.get("no");
        String sex = (String) param.get("sex");

        Page<Countproduct> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Countproduct> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(no) && !"null".equals(no)) {
            lambdaQueryWrapper.like(Countproduct::getNo, no);
        }

        IPage<Countproduct> data = countproductService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + data.getTotal());

        return Result.success(data.getRecords(), data.getTotal());
    }
}
